<?php
$_DATEBASE_CONN_ERR = 1000;
$_RULE_BREAK = 1001;
$_ILLEGAL_CHAR = 1002;

function chkndata($d)
{
	if(strlen($d) < 4 || strlen($d) > 16)
		godie($GLOBALS['_RULE_BREAK']);
	chksymbol($d);
}
function godie($str)
{
	die(json_encode(array('errorcode'=>$str)));
}
function chksymbol($str)
{
	$nss = array("\'", "\"", "*", "<", ">", ",", "[", "]", "$");
	foreach ($nss as $value)
		if(strpos($str, $value) != FALSE)
			godie($GLOBALS['_ILLEGAL_CHAR']);
}
function outputln($str)
{
	echo "</br>".$str;
}
function safelog($username, $action, $conn)
{
	$ipaddr = $_SERVER['REMOTE_ADDR'];
	$time = time();
	$result = mysql_query("insert into safelog (username, ipaddr, updatetime, action) values ('$username', '$ipaddr', '$time', '$action')", $conn);
}
function connect2db()
{
	$conn = mysql_connect("localhost:3306", "admin8X4yygd", "YJNz4Jdpnb8h");
	if(!$conn)
		die($GLOBALS['_DATEBASE_CONN_ERR']);
	mysql_select_db("connect", $conn);
	return $conn;
}
function checkEmail($email){
	return filter_var($email, FILTER_VALIDATE_EMAIL);
}
function mysqli_connect2db(){
	return new mysqli("localhost:3306", "admin8X4yygd", "YJNz4Jdpnb8h", "connect");
}
function pdo_root_connect(){
	$dbms='mysql';     //数据库类型
	$host='127.9.88.2:3306'; //数据库主机名
	$dbName='connect';    //使用的数据库
	$user='admin8X4yygd';      //数据库连接用户名
	$pass='YJNz4Jdpnb8h';          //对应的密码
	$dsn="$dbms:host=$host;dbname=$dbName";
	
	try {
		$dbh = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true)); //初始化一个PDO对象
		return $dbh;
	}catch (PDOException $e) {
		echo $e->getMessage();
		return false;
	}
}
function pdo_root_connect_short(){
	$dbms='mysql';
	$host='127.9.88.2:3306';
	$dbName='connect';
	$user='admin8X4yygd';
	$pass='YJNz4Jdpnb8h';
	$dsn="$dbms:host=$host;dbname=$dbName";
	
	try {
		$dbh = new PDO($dsn, $user, $pass); //初始化一个PDO对象
		return $dbh;
	}catch (PDOException $e) {
		return false;
	}
}
function pdo_createtoken($username){
	$qid = uniqid('ac_'.$username);
	return substr(md5($qid), 8, 16);
}
function getRunTime()
{
	list($msec, $sec) = explode( ' ', microtime());
	return (float)$sec+(float)$msec;
}
?>